﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using EFFC.Net.Interface;
using EFFC.Net.Exceptions;
using System.Xml.Serialization;
using System.ServiceModel;


namespace EFFC.Net.Parameters
{
    
    [Serializable]
    public class PElement : ISerializable
    {
        string name;
        Type t = typeof(string);
        object value;
        string xmlns = "http://www.WITS.com/Frame/Base/Parameters";
        public PElement()
        {
        }
        /// <summary>
        /// 初始化，默認為string類型數據
        /// </summary>
        /// <param name="name"></param>
        public PElement(string name)
        {
            this.name = name;
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="name"></param>
        public PElement(string name, Type t)
        {
            this.name = name;
            this.t = t;
        }
        /// <summary>
        /// 參數名稱
        /// </summary>
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        /// <summary>
        ///  數據類型，默認為string類型
        /// </summary>
        public Type PType
        {
            get
            {
                return t;
            }
            set
            {
                t = value;
            }
        }
        /// <summary>
        /// 參數的值，如果賦值的value与PType不匹配則拋出異常
        /// </summary>
        public object Value
        {
            get
            {
                return value;
            }
            set
            {
                bool isType = false;
                if (value == null)
                {
                    isType = true;
                }
                else if (value.GetType().Equals(t))
                {
                    isType = true;
                }
                else if (value.GetType().BaseType.Equals(t))
                {
                    isType = true;
                }
                else
                {
                    foreach (Type tt in value.GetType().GetInterfaces())
                    {
                        if (tt.Equals(t))
                        {
                            isType = true;
                            break;
                        }
                    }
                }

                if (isType)
                {
                    this.value = value;
                }
                else
                {
                    throw new EFFC.Net.Exceptions.BizException(EFFC.Net.Common.ExceptionMessage.ParameterStd.Invalid_Type_Error, value.ToString(), t.FullName);
                }
            }
        }
        [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (info != null)
            {
                info.AddValue("PName#", name);
                info.AddValue("PType#", t.FullName);
                info.AddValue("PValue#", value);
            }
        }

        private PElement(SerializationInfo info, StreamingContext context)
        {
            this.Name = info.GetString("PName#");
            this.PType = Type.GetType(info.GetString("PType#"));
            this.Value = info.GetValue("PValue#", this.PType);
        }

        
    }
}
